package com.micai.springboot.service;

import com.micai.springboot.util.pay.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/28 17:37
 * @Description: 基于http client 4.2.3叮咚云相关接口
 */
public class DingdongCloudApis {

    // 查账户信息的http地址
    private static String URL_GET_USER_INFO = "https://api.dingdongcloud.com/v1/sms/userinfo";

    // 查询账户余额的http地址
    private static String URL_GET_BALANCE = "https://api.dingdongcloud.com/v1/sms/querybalance";

    // 验证码短信发送接口的http地址
    private static String URL_SEND_YZM = "https://api.dingdongcloud.com/v1/sms/sendyzm";

    // 语音验证码发送接口的http地址
    private static String URL_SEND_YYYZM = "https://api.dingdongcloud.com/v1/sms/sendyyyzm";

    // 通知短信发送接口的http地址
    private static String URL_SEND_TZ = "https://api.dingdongcloud.com/v1/sms/sendtz";

    // 营销短信发送接口的http地址
    private static String URL_SEND_YX = "https://api.dingdongcloud.com/v1/sms/sendyx";

    // 编码格式。发送编码格式统一用UTF-8
    private static String ENCODING = "UTF-8";

    /**
     * 发送验证码短信
     *
     * @param apikey
     *            apikey
     * @param mobile
     *            手机号码(唯一，不许多个)
     * @param content
     *            短信发送内容（必须经过utf-8格式编码)
     * @return json格式字符串
     */
    public static String sendYzm(String apikey, String mobile, String content) {
        if (StringUtils.isNotBlank(content)) {
            try {
                content = URLEncoder.encode(content, ENCODING);
                Map<String, String> params = new HashMap<String, String>();
                params.put("apikey", apikey);
                params.put("mobile", mobile);
                params.put("content", content);
                return HttpUtil.postData(URL_SEND_YZM, params.toString());
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
